import 'package:flutter/material.dart';
import 'package:flutter_web_study/provider/base_provider.dart';
import '../view_model/search_view_model.dart';

class SearchPage extends StatefulWidget {
  String? tagPage;
  String? brand;
  String? series;

  SearchPage({Key? key, this.tagPage, this.brand, this.series})
      : super(key: key);

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<SearchViewModel>(
      model:
          SearchViewModel(context, widget.tagPage, widget.brand, widget.series),
      onModelReady: (model) {
        model.initView();
      },
      builder: (BuildContext context, viewModel, child) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.red,
            title: TextField(
              controller: viewModel.textEditingController,
              decoration: const InputDecoration(border: OutlineInputBorder()),
              onChanged: (value) {
                viewModel.getViewData(value);
              },
            ),
            actions: [
              MaterialButton(
                  color: Colors.blue,
                  textColor: Colors.white,
                  child: Text("确定"),
                  onPressed: () => viewModel.finishValue()),
            ],
          ),
          body: viewModel.list.length > 0
              ? ListView.builder(
                  shrinkWrap: true,
                  itemCount: viewModel.list.length,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      onTap: () => viewModel.selectListValue(index),
                      child: Container(
                        alignment: Alignment.centerLeft,
                        color: Colors.amberAccent,
                        margin: EdgeInsets.all(5),
                        padding: EdgeInsets.all(10),
                        child: viewModel.tagPage == "0"
                            ? Column(
                                children: [
                                  Text(
                                      "品牌名称:${viewModel.list[index]["brandName"]}",
                                      style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 15,
                                          fontWeight: FontWeight.bold)),
                                  Text(
                                      "商品系列:${viewModel.list[index]["series"]}"),
                                  Text("色号:${viewModel.list[index]["color"]}")
                                ],
                              )
                            : Text("${viewModel.list[index]}"),
                      ),
                    );
                  })
              : Center(
                  child: Text("没有搜索到"),
                ),
        );
      },
    );
  }
}
